# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/9/28
desc: 
sample: 
"""
#get post 协议
# get url
# post body
# post json
#普通数据 json http状态码200 业务判断 提取code  message
#数据关联 cookie  token
import json
import re

import requests
from bs4 import BeautifulSoup

host = 'http://121.11.97.201:9088/pinter/com/'
def test_get():
    response = requests.get(url=f'{host}getSku?id=1')
    print(response)
    assert response.status_code==200
    print(response.text)
    res_dict= json.loads(response.text) #字符串
    #json字符串 -》 字典
    print(type(res_dict))
    assert res_dict['code']=='0'

def test_get2():
    url_params = {'id':1}
    response = requests.get(url=f'{host}getSku',params=url_params)
    assert response.status_code == 200
    res_dict = response.json() #方法
    print(res_dict)
    print(type(res_dict))
    assert res_dict['code']=='0'

def test_get3():
    url_params = {'id':1}
    response = requests.get(url='http://www.baidu.com',params=url_params)
    assert response.status_code == 200
    html = response.text # 特殊方法@property
    reg='<title>(.*)</title>'
    print(re.findall(reg,html)[0])
    # soup = BeautifulSoup(response.text, 'html.parser')
    # print(soup)
    # # 提取所有链接
    # links = [link.get('href') for link in soup.find_all('a')]
    # print(links)


def test_xml():
    url ='http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx'
    #协议格式
    xml='<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><getMobileCodeInfo xmlns="http://WebXml.com.cn/">' \
        '<mobileCode>18810983323</mobileCode><userID></userID>' \
        '</getMobileCodeInfo></soap:Body></soap:Envelope>'
    xml_header = {'Content-type': 'text/xml'}
    response = requests.post(url=url,data=xml,headers=xml_header)
    assert response.status_code == 200
    xml = response.text # 特殊方法@property
    print(xml)


def test_post():
    post_params={'userName':'admin','password':'1234'}
    response = requests.post(url=f'{host}login',data=post_params)
    print(response)

def test_postjson():
    #协议头
    post_params={"userName":"test","password":"1234","gender":1,
                 "phoneNum":"110","email":"beihe@163.com","address":"Beijing"}
    response = requests.post(url=f'{host}register',json=post_params)
    print(response)

def test_postFile():
    #协议头
    file = {'file': open(r'D:\抖音.txt', 'rb')}
    response = requests.post(url='http://121.11.97.201:9088//pinter/file/api/upload',files=file)
    print(response)

def is_json(str):
    try:
        json.loads(str)
        return True
    except:
        return False

# test_xml()
# test_get()
# test_get2()
# test_get3()
# test_post()
# test_postjson()
# test_postFile()